// pages/sign_lib/sign_lib.js
const baseURL = 'http://100.2.178.56:8080'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        reserve: null,
        signLost_count: 0,
        signLost_time: '',
        reserveCancel_count: 0,
        reserveCancel_time: '',
        seaNum: '',
        floNum: '',
        teacher: null,

        // 下拉框
        selectshow: false,
        selectData: ['班级', '姓名', '学号', '学院'],
        index: 0,
        //搜索框
        input_check: false,
        list: [],
        confirmValue: '',
        //搜索显示
        //班级
        list_grade_show: [],
        //学院
        list_academy_show: [],
        show_check: false,
        //学号查询出来的的内容
        student_show: null,
        student_grade_show: null,
        student_reserve_list_show: [],
        //返回索引
        before_index: [-1],
        //
    },
    //========================================搜索框
    //键盘输入时实时调用搜索方法
    input(e) {
        // console.log(e)
        this.setData({
            input_check: true,
            show_check: false
        })
        this.search(e.detail.value)
    },
    //点击完成按钮时触发
    confirm(e) {
        this.search(e.detail.value)
    },
    search(key) {
        var that = this;
        //从本地缓存中异步获取指定 key 的内容
        var list = wx.getStorage({
            key: 'list',
            //从Storage中取出存储的数据
            success: function (res) {
                // console.log(res)
                if (key == '') { //用户没有输入时全部显示
                    that.setData({
                        list: res.data
                    })
                    return;
                }
                var arr = []; //临时数组，用于存放匹配到的数组
                for (let i in res.data) {
                    res.data[i].show = false; //所有数据隐藏
                    if (res.data[i].search.indexOf(key) >= 0) {
                        res.data[i].show = true; //让匹配到的数据显示
                        arr.push(res.data[i])
                    }
                }
                if (arr.length == 0) {
                    that.setData({
                        list: [{
                            show: true,
                            name: '没有相关数据！'
                        }]
                    })
                } else {
                    that.setData({
                        list: arr
                    })
                }
            },
        })
    },
    getItem_click(e) {
        console.log(e.currentTarget.dataset.value);
        this.setData({
            input_check: false,
            confirmValue: e.currentTarget.dataset.value
        })
    },
    confirm: function (e) {

        this.setData({
            show_check: true
        })
        var that = this
        console.log("confirm")
        this.data.list.forEach(element => {
            element.show = false;
        });
        this.setData({
            list: this.data.list
        })
        var Index = this.data.index
        console.log(Index);

        if (Index == 0) {
            this.getStudentListByGraName_confirm(e.detail.value);
        } else if (Index == 1) {
            this.getStudentByStuName_confirm(e.detail.value)
        } else if (Index == 2) {
            this.getStudentByStuId_confirm(e.detail.value)
        } else if (Index == 3) {
            this.getGradeListByAcaName_confirm(e.detail.value);
        }


    },
    getStudentListByGraName_confirm: function (graName) {
        var that = this
        //获取该班级的所有学生
        wx.request({
            url: baseURL + '/student/graName',
            method: "GET",
            data: {
                'graName': graName
            },
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                if (res.data.code == 0) {
                    that.setData({
                        list_grade_show: res.data.data.list
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        duration: 2000,
                        icon: 'none'
                    })
                }
            }
        })
    },
    getGradeListByAcaName_confirm: function (acaName) {
        var that = this
        //获取该学院的所有班级
        wx.request({
            url: baseURL + '/grade/acaName',
            method: "GET",
            data: {
                'acaName': acaName
            },
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                if (res.data.code == 0) {
                    that.setData({
                        list_academy_show: res.data.data.list
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        duration: 2000,
                        icon: 'none'
                    })
                }
            }
        })
    },
    getStudentByStuId_confirm: function (stuId) {
        var that = this
        //获取该班级的所有学生
        wx.request({
            url: baseURL + '/student/stuId',
            method: "GET",
            data: {
                'stuId': stuId
            },
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                if (res.data.code == 0) {
                    that.setData({
                        student_show: res.data.data.student
                    });
                    //获取该生的信用分
                    wx.request({
                        url: baseURL + '/honestStudent/stuId',
                        method: "GET",
                        data: {
                            'stuId': stuId
                        },
                        header: {
                            'Authorization': getApp().token
                        },
                        success: function (res) {
                            if (res.data.code == 0) {
                                that.setData({
                                    student_grade_show: res.data.data.grade
                                })
                            }
                        }
                    });
                    //获取预约记录
                    wx.request({
                        url: baseURL + '/studentSeatRelation/stuId/list',
                        method: "GET",
                        header: {
                            'Authorization': getApp().token
                        },
                        data: {
                            "stuId": stuId,

                        },
                        success: function (res) {
                            that.setData({
                                student_reserve_list_show: res.data.data.list
                            })
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        duration: 2000,
                        icon: 'none'
                    })
                }
            }
        })
    },
    getStudentByStuName_confirm: function (stuName) {
        var that = this
        var stuId = '';
        wx.request({
            url: baseURL + '/student/stuName',
            method: "GET",
            data: {
                'stuName': stuName
            },
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                if (res.data.code == 0) {
                    stuId = res.data.data.student.stuId

                    //获取该班级的所有学生通过姓名
                    wx.request({
                        url: baseURL + '/student/stuId',
                        method: "GET",
                        data: {
                            'stuId': stuId
                        },
                        header: {
                            'Authorization': getApp().token
                        },
                        success: function (res) {
                            if (res.data.code == 0) {
                                that.setData({
                                    student_show: res.data.data.student
                                });
                                //获取该生的信用分
                                wx.request({
                                    url: baseURL + '/honestStudent/stuId',
                                    method: "GET",
                                    data: {
                                        'stuId': stuId
                                    },
                                    header: {
                                        'Authorization': getApp().token
                                    },
                                    success: function (res) {
                                        if (res.data.code == 0) {
                                            that.setData({
                                                student_grade_show: res.data.data.grade
                                            })
                                        }
                                    }
                                });
                                //获取预约记录
                                wx.request({
                                    url: baseURL + '/studentSeatRelation/stuId/list',
                                    method: "GET",
                                    header: {
                                        'Authorization': getApp().token
                                    },
                                    data: {
                                        "stuId": stuId,

                                    },
                                    success: function (res) {
                                        that.setData({
                                            student_reserve_list_show: res.data.data.list
                                        })
                                    }
                                })
                            } else {
                                wx.showToast({
                                    title: res.data.message,
                                    duration: 2000,
                                    icon: 'none'
                                })
                            }
                        }
                    })
                }
            }
        })

    },
    //学生预约记录----预约详情跳转
    show_detail_click: function (e) {
        if (this.data.index == 0) {
            this.getStudentByStuId_confirm(e.currentTarget.dataset.value.stuId)
            this.data.before_index.push(0)
            this.setData({
                index: 2,
                before_index: this.data.before_index
            })
        } else if (this.data.index == 3) {
            this.getStudentListByGraName_confirm(e.currentTarget.dataset.value.graName)
            this.data.before_index.push(3)
            this.setData({
                index: 0,
                before_index: this.data.before_index
            })
        } else if (this.data.index == 2 || this.data.index == 1) {
            var reserve_detail = JSON.stringify(e.currentTarget.dataset.value);
            wx.navigateTo({
                url: '/pages/sign_lib/sign_record/sign_detail/sign_detail?reserve_detail=' + reserve_detail,
            })
        }
    },
    back_index_click: function () {
        var Index = this.data.before_index.pop();
        this.setData({
            index: Index,
            before_index: this.data.before_index
        })
    },
    //=====================================下拉框==========================
    // 点击下拉显示框
    selectTap() {
        this.setData({
            selectShow: !this.data.selectShow
        });
    },
    // 点击下拉列表
    optionTap(e) {
        let Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
        this.setData({
            index: Index,
            selectShow: !this.data.selectShow
        });
        // ['班级', '姓名', '学号', '学院'],
        if (Index == 0) {
            this.getGradeListByIndex();
        } else if (Index == 1) {
            this.getNameListByIndex();
        } else if (Index == 2) {
            this.getstuIdListByIndex();
        } else if (Index == 3) {
            this.getAcademyListByIndex();
        }

    },


    getGradeListByIndex() {
        var that = this;
        //下拉框初始化，默认第一个选项为班级
        //获取所有班级
        wx.request({
            url: baseURL + '/grade/list',
            method: "GET",
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                //封装list
                var list = res.data.data.list;

                var list_new = [];
                list.forEach(element => {
                    list_new.push({
                        "name": element.graName,
                        "show": true,
                        "search": element.graName
                    })
                });
                that.setStorageByList(list_new)

            }
        })
    },
    getAcademyListByIndex() {
        var that = this;
        //下拉框初始化，默认第一个选项为班级
        //获取所有班级
        wx.request({
            url: baseURL + '/academy/list',
            method: "GET",
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                //封装list
                var list = res.data.data.list;

                var list_new = [];
                list.forEach(element => {
                    list_new.push({
                        "name": element.acaName,
                        "show": true,
                        "search": element.acaName
                    })
                });
                that.setStorageByList(list_new)

            }
        })
    },
    getstuIdListByIndex() {
        var that = this;
        //下拉框初始化，默认第一个选项为班级
        //获取所有班级
        wx.request({
            url: baseURL + '/student/list',
            method: "GET",
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                //封装list
                var list = res.data.data.list;

                var list_new = [];
                list.forEach(element => {
                    list_new.push({
                        "name": element.stuId,
                        "show": true,
                        "search": element.stuId
                    })
                });
                that.setStorageByList(list_new)

            }
        })
    },
    getNameListByIndex() {
        var that = this;
        //下拉框初始化，默认第一个选项为班级
        //获取所有班级
        wx.request({
            url: baseURL + '/student/list',
            method: "GET",
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                //封装list
                var list = res.data.data.list;

                var list_new = [];
                list.forEach(element => {
                    list_new.push({
                        "name": element.stuName,
                        "show": true,
                        "search": element.stuName
                    })
                });
                that.setStorageByList(list_new)

            }
        })
    },
    setStorageByList(list_new) {
        console.log(list_new);
        wx.setStorage({
            key: 'list',
            data: list_new
        })
        this.setData({
            list: list_new
        })
    },
    // ==========================================
    sign_record: function () {

        wx.navigateTo({
            url: '/pages/sign_lib/sign_record/sign_record?stuUseId=' + getApp().globalStuUseId,
        })


    },
    signback_click: function () {
        var that = this;
        wx.scanCode({
            success: (res) => {
                console.log("qr")
                console.log(res)
                wx.request({
                    url: baseURL + '/studentSeatRelation/signback',
                    method: "GET",
                    header: {
                        'Authorization': getApp().token
                    },
                    data: {
                        "seaId": res.result,
                        "stuSeaId": that.data.reserve.stuSeaId,

                    },
                    success: function (res1) {
                        if (res1.data.code == 0) {
                            wx.showToast({
                                title: '签退成功',
                                icon: 'success',
                                duration: 2000
                            })
                            getApp().user_reserve.signState = true
                            that.setData({
                                reserve: getApp().user_reserve.signState
                            })
                            that.onShow();
                        } else {
                            wx.showToast({
                                title: res1.data.message,
                                icon: 'none',
                                duration: 3000
                            })
                        }
                    }
                })
            }
        })
    },
    // mark: 签到点击
    sign_click: function () {
        console.log("sign_click")
        var that = this;
        wx.scanCode({
            success: (res) => {
                console.log("qr")
                console.log(res)
                wx.request({
                    url: baseURL + '/studentSeatRelation/sign',
                    method: "GET",
                    header: {
                        'Authorization': getApp().token
                    },
                    data: {
                        "seaId": res.result,
                        "stuSeaId": that.data.reserve.stuSeaId,

                    },
                    success: function (res1) {
                        if (res1.data.code == 0) {
                            console.log("sign_click->success")
                            wx.showToast({
                                title: '签到成功',
                                icon: 'success',
                                duration: 2000
                            })
                            getApp().user_reserve.signState = true
                            that.setData({
                                reserve: getApp().user_reserve.signState
                            })
                            that.onShow();
                        } else {
                            wx.showToast({
                                title: res1.data.message,
                                icon: 'none',
                                duration: 3000
                            })
                        }
                    }
                })
            }
        })
    },
    getReserveState: function () {
        var that = this
        //获取选座状态
        wx.request({
            url: baseURL + '/studentSeatRelation/main/reserve',
            method: "GET",
            header: {
                'Authorization': getApp().token
            },
            data: {
                stuId: getApp().globalUserInfo.stuId,
            },
            success: function (res) {
                console.log("reserve")
                console.log(res.data.data)
                var resData = res.data;
                if (resData.code == 0) {
                    console.log("reserve::")
                    that.setData({
                        reserve: resData.data.reserve,
                        seaNum: resData.data.seaNum,
                        floNum: resData.data.floNum
                        // reserveState: resData.data.reserve.reserveState
                    });
                    getApp().user_reserve = resData.data.reserve
                    getApp().seaNum = that.data.seaNum
                    getApp().floNum = that.data.floNum

                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        if (getApp().globalUserInfo_tea != null) {
            //下拉框初始化，默认第一个选项为班级
            //获取所有班级
            wx.request({
                url: baseURL + '/grade/list',
                method: "GET",
                header: {
                    'Authorization': getApp().token
                },
                success: function (res) {
                    //封装list
                    var list = res.data.data.list;

                    var list_new = [];
                    list.forEach(element => {
                        list_new.push({
                            "name": element.graName,
                            "show": true,
                            "search": element.graName
                        })
                    });
                    console.log(list_new);
                    wx.setStorage({
                        key: 'list',
                        data: list_new
                    })
                    that.setData({
                        list: list_new
                    })

                }
            })
        }

        //============================================
        if (getApp().globalUserInfo_tea == null) {
            this.getReserveState();
            var that = this
            this.setData({
                reserve: getApp().user_reserve,
                seaNum: getApp().seaNum,
                floNum: getApp().floNum
            })

            //缺到次数
            wx.request({
                url: baseURL + '/honestStudent/hon',
                method: "GET",
                header: {
                    'Authorization': getApp().token
                },
                data: {
                    "hon_id": 2,
                    "stu_use_id": getApp().globalStuUseId
                },
                success: function (res) {
                    var resData = res.data;
                    if (resData.code == 0) {
                        that.setData({

                            signLost_count: resData.data.count,
                            signLost_time: resData.data.time,
                        });
                    }
                }
            })

            //缺到次数
            wx.request({
                url: baseURL + '/honestStudent/hon',
                method: "GET",
                header: {
                    'Authorization': getApp().token
                },
                data: {
                    "hon_id": 1,
                    "stu_use_id": getApp().globalStuUseId
                },
                success: function (res) {
                    var resData = res.data;
                    if (resData.code == 0) {
                        that.setData({

                            reserveCancel_count: resData.data.count,
                            reserveCancel_time: resData.data.time,
                        });
                    }
                }
            })
        } else {
            this.setData({
                teacher: getApp().globalUserInfo_tea
            })
        }


    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})